using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using Codice.Utils;
using Codice.Client.Extension.Types;

namespace Codice.Client.Extension
{
	// from extenstioncommon.dll
	public class RedmineConfigurationPanel : Codice.Client.Extension.BaseConfigurationPanel
	{
        private Codice.I3.I3Label apiKeyLabel;
        private Codice.I3.I3TextBox serverTextbox;
        private Codice.I3.I3TextBox userTextbox;
        private Codice.I3.I3Label userLabel;
        private Codice.I3.I3CheckBox useApiKeyCheckbox;
        private Codice.I3.I3TextBox passswordTextBox;
        private Codice.I3.I3Label passwordLabel;
        private Codice.I3.I3Label i3Label2;

        private System.ComponentModel.IContainer components = null;

        private Codice.I3.I3TextBox apiKeyTextBox;
        private Codice.I3.I3Label serverLabel;
        private const string ISSUE_RELATIVE_URL = "/issues";

		public RedmineConfigurationPanel ()
		{
			InitializeComponent();
			InitializeTheme();
            InitializeMembers();
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support. 
		/// Can not modify the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent ()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RedmineConfigurationPanel));
            this.i3Label2 = new Codice.I3.I3Label();
            this.apiKeyLabel = new Codice.I3.I3Label();
            this.serverTextbox = new Codice.I3.I3TextBox();
            this.userTextbox = new Codice.I3.I3TextBox();
            this.userLabel = new Codice.I3.I3Label();
            this.useApiKeyCheckbox = new Codice.I3.I3CheckBox();
            this.passswordTextBox = new Codice.I3.I3TextBox();
            this.passwordLabel = new Codice.I3.I3Label();
            this.serverLabel = new Codice.I3.I3Label();
            this.apiKeyTextBox = new Codice.I3.I3TextBox();
            this.mediumPanel.SuspendLayout();
            this.topPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // mediumPanel
            // 
            this.mediumPanel.Controls.Add(this.apiKeyTextBox);
            this.mediumPanel.Controls.Add(this.serverLabel);
            this.mediumPanel.Controls.Add(this.passswordTextBox);
            this.mediumPanel.Controls.Add(this.passwordLabel);
            this.mediumPanel.Controls.Add(this.useApiKeyCheckbox);
            this.mediumPanel.Controls.Add(this.userTextbox);
            this.mediumPanel.Controls.Add(this.userLabel);
            this.mediumPanel.Controls.Add(this.apiKeyLabel);
            this.mediumPanel.Controls.Add(this.serverTextbox);
            this.mediumPanel.Controls.Add(this.i3Label2);
            resources.ApplyResources(this.mediumPanel, "mediumPanel");
            this.mediumPanel.Controls.SetChildIndex(this.testConnectionButton, 0);
            this.mediumPanel.Controls.SetChildIndex(this.i3Label2, 0);
            this.mediumPanel.Controls.SetChildIndex(this.serverTextbox, 0);
            this.mediumPanel.Controls.SetChildIndex(this.apiKeyLabel, 0);
            this.mediumPanel.Controls.SetChildIndex(this.userLabel, 0);
            this.mediumPanel.Controls.SetChildIndex(this.userTextbox, 0);
            this.mediumPanel.Controls.SetChildIndex(this.useApiKeyCheckbox, 0);
            this.mediumPanel.Controls.SetChildIndex(this.passwordLabel, 0);
            this.mediumPanel.Controls.SetChildIndex(this.passswordTextBox, 0);
            this.mediumPanel.Controls.SetChildIndex(this.serverLabel, 0);
            this.mediumPanel.Controls.SetChildIndex(this.apiKeyTextBox, 0);
            // 
            // testConnectionButton
            // 
            resources.ApplyResources(this.testConnectionButton, "testConnectionButton");
            // 
            // i3Label2
            // 
            this.i3Label2.AutoWidth = false;
            this.i3Label2.BackColor = System.Drawing.Color.Transparent;
            this.i3Label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            resources.ApplyResources(this.i3Label2, "i3Label2");
            this.i3Label2.ForeColor = System.Drawing.Color.Black;
            this.i3Label2.Name = "i3Label2";
            this.i3Label2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            // 
            // apiKeyLabel
            // 
            this.apiKeyLabel.AutoWidth = false;
            this.apiKeyLabel.BackColor = System.Drawing.Color.Transparent;
            this.apiKeyLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.apiKeyLabel.ForeColor = System.Drawing.Color.Black;
            resources.ApplyResources(this.apiKeyLabel, "apiKeyLabel");
            this.apiKeyLabel.Name = "apiKeyLabel";
            this.apiKeyLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            // 
            // serverTextbox
            // 
            resources.ApplyResources(this.serverTextbox, "serverTextbox");
            this.serverTextbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.serverTextbox.DelayedTextChangedMilliseconds = 250;
            this.serverTextbox.Name = "serverTextbox";
            this.serverTextbox.WatermarkText = null;
            // 
            // userTextbox
            // 
            resources.ApplyResources(this.userTextbox, "userTextbox");
            this.userTextbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.userTextbox.DelayedTextChangedMilliseconds = 250;
            this.userTextbox.Name = "userTextbox";
            this.userTextbox.WatermarkText = null;
            // 
            // userLabel
            // 
            this.userLabel.AutoWidth = false;
            this.userLabel.BackColor = System.Drawing.Color.Transparent;
            this.userLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.userLabel.ForeColor = System.Drawing.Color.Black;
            resources.ApplyResources(this.userLabel, "userLabel");
            this.userLabel.Name = "userLabel";
            this.userLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            // 
            // useApiKeyCheckbox
            // 
            this.useApiKeyCheckbox.BackColor = System.Drawing.Color.White;
            this.useApiKeyCheckbox.Checked = true;
            this.useApiKeyCheckbox.CheckState = System.Windows.Forms.CheckState.Checked;
            this.useApiKeyCheckbox.ForceDrawFocusBorder = false;
            this.useApiKeyCheckbox.ForeColor = System.Drawing.Color.Black;
            resources.ApplyResources(this.useApiKeyCheckbox, "useApiKeyCheckbox");
            this.useApiKeyCheckbox.Name = "useApiKeyCheckbox";
            this.useApiKeyCheckbox.UseVisualStyleBackColor = false;
            // 
            // passswordTextBox
            // 
            resources.ApplyResources(this.passswordTextBox, "passswordTextBox");
            this.passswordTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.passswordTextBox.DelayedTextChangedMilliseconds = 250;
            this.passswordTextBox.Name = "passswordTextBox";
            this.passswordTextBox.WatermarkText = null;
            // 
            // passwordLabel
            // 
            resources.ApplyResources(this.passwordLabel, "passwordLabel");
            this.passwordLabel.AutoWidth = false;
            this.passwordLabel.BackColor = System.Drawing.Color.Transparent;
            this.passwordLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.passwordLabel.ForeColor = System.Drawing.Color.Black;
            this.passwordLabel.Name = "passwordLabel";
            this.passwordLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            // 
            // serverLabel
            // 
            this.serverLabel.AutoWidth = false;
            this.serverLabel.BackColor = System.Drawing.Color.Transparent;
            this.serverLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.serverLabel.ForeColor = System.Drawing.Color.Black;
            resources.ApplyResources(this.serverLabel, "serverLabel");
            this.serverLabel.Name = "serverLabel";
            this.serverLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            // 
            // apiKeyTextBox
            // 
            resources.ApplyResources(this.apiKeyTextBox, "apiKeyTextBox");
            this.apiKeyTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.apiKeyTextBox.DelayedTextChangedMilliseconds = 250;
            this.apiKeyTextBox.Name = "apiKeyTextBox";
            this.apiKeyTextBox.WatermarkText = null;
            // 
            // RedmineConfigurationPanel
            // 
            this.Name = "RedmineConfigurationPanel";
            resources.ApplyResources(this, "$this");
            this.mediumPanel.ResumeLayout(false);
            this.mediumPanel.PerformLayout();
            this.topPanel.ResumeLayout(false);
            this.topPanel.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void InitializeMembers()
        {
            passswordTextBox.PasswordChar = mPasswordChar;
        }

        public override BaseExtensionConfiguration GetConfiguration()
        {
            ValidateFields();
            RedmineExtensionConfiguration result = new RedmineExtensionConfiguration();
            FillCommonFields(result);
            result = UpdateConfiguration(result);
            return result;
        }

        protected override void ValidateFields()
        {
            base.ValidateFields();

            CheckField(serverLabel.Text, serverTextbox.Text);
            if (useApiKeyCheckbox.Checked)
            {
                CheckField(apiKeyLabel.Text, apiKeyTextBox.Text);
            }
            else
            {
                CheckField(userLabel.Text, userLabel.Text);
                CheckField(passwordLabel.Text, passswordTextBox.Text);
            }
        }

        private RedmineExtensionConfiguration UpdateConfiguration(RedmineExtensionConfiguration result)
        {
            result.Url = RemoveLastSeparator(serverTextbox.Text);
            result.ApiKey = apiKeyTextBox.Text;
            result.ShowIssueUrl = GetShowIssueUrl(RemoveLastSeparator(serverTextbox.Text));
            result.User = userTextbox.Text;
            result.Password = CryptoServices.GetEncryptedPassword(passswordTextBox.Text);
            return result;
        }


        private string RemoveLastSeparator(string s)
        {
            //Remove last '/' (needed)
            if (s != null && s.EndsWith("/"))
            {
                return s.Substring(0, s.Length - 1);
            }

            return s;
        }

        private string GetShowIssueUrl(string baseUrl)
        {
            return string.Concat(baseUrl, ISSUE_RELATIVE_URL);
        }


        public override void SetConfiguration(BaseExtensionConfiguration configuration)
        {
            base.SetConfiguration(configuration);

            RedmineExtensionConfiguration redmineConfiguration = configuration as RedmineExtensionConfiguration;

            if (redmineConfiguration == null)
                return;

            serverTextbox.Text = GetBaseRedmineUrl(redmineConfiguration.ShowIssueUrl);
            apiKeyTextBox.Text = redmineConfiguration.ApiKey;
            userTextbox.Text = redmineConfiguration.User;
            passswordTextBox.Text = CryptoServices.GetDecryptedPassword(redmineConfiguration.Password);
            useApiKeyCheckbox.Checked = useApiKey(redmineConfiguration.ApiKey);
        }

        private string GetBaseRedmineUrl(string showIssueUrl)
        {
            if (showIssueUrl == null || showIssueUrl == string.Empty)
                return string.Empty;

            int index = showIssueUrl.IndexOf(ISSUE_RELATIVE_URL);

            if (index < 0)
                return string.Empty;

            return showIssueUrl.Substring(0, index);
        }

        private bool useApiKey(string ApiKey)
        {
            if (ApiKey == null || ApiKey == string.Empty)
                return false;

            return (ApiKey.Length > 0);
        }

        private void useApiKeyCheckbox_CheckedChanged(object sender, System.EventArgs e)
        {
            if (useApiKeyCheckbox.Checked)
            {
                apiKeyTextBox.Enabled = true;
            }
            else
            {
                userTextbox.Enabled = false;
                passswordTextBox.Enabled = false;
            }
            OnConfigurationDirty(sender, e);
        }

        private void Textbox_TextChanged(object sender, System.EventArgs e)
        {
            OnConfigurationDirty(sender, e);
        }
	}
}

